package com.sgb.auction.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.sgb.auction.R;
import com.sgb.auction.adapter.AuctionHallAdapter;
import com.sgb.auction.base.BaseApi;
import com.sgb.auction.databinding.ActivityAuctionHallBinding;
import com.sgb.auction.entity.PublishListBean;
import com.sgb.auction.entity.RequestBean;
import com.sgb.auction.fragment.AuctionHallFragment;
import com.sgb.auction.viewmodule.AuctionHallModel;
import com.sgb.link.router.PathLogin;
import com.swgk.core.base.AppActivity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.recyclerview.entity.PublishMenuEntity;
import com.swgk.core.util.CommonUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.pop.HomeZhaoCaiPublishMenuPop;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/11/19
 * AUTHOR:F_Q
 * Description:拍卖大厅
 */
public class AuctionHallActivity extends AppActivity {
    private static HomeZhaoCaiPublishMenuPop homezhaocaipublishmenupop;
    private RequestBean requestBean = new RequestBean();
    private AuctionHallModel mModel;
    private ActivityAuctionHallBinding mBinding;
    private AuctionHallAdapter auctionhalladapter;
    private boolean isLoadMore = false;
    private ArrayList<Fragment> mFragments;
    private SlidingFragmentPagerAdapter mPagerAdapter;
    private ArrayList<String> mTitles;
    private boolean isPublish = false;

    public static void start(Context context,String type) {
        Intent intent = new Intent();
        intent.putExtra("type",type);
        intent.setClass(context, AuctionHallActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_auction_hall);
        mModel = new ViewModelProvider(this).get(AuctionHallModel.class);
        mModel.setActivity(AuctionHallActivity.this);
        mBinding.ivBack.setOnClickListener(v -> finish());
        String type = getIntent().getStringExtra("type");
        if (TextUtils.equals(type,"1")){
            mBinding.vLine.setVisibility(View.VISIBLE);
            mBinding.rightView.setVisibility(View.VISIBLE);
        }else {
            mBinding.vLine.setVisibility(View.GONE);
            mBinding.rightView.setVisibility(View.GONE);
        }
        mBinding.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    mBinding.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }
        });
        mBinding.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.etView.setText("");
                mBinding.tvSearchDelete.setVisibility(View.GONE);
                initDataList();
            }
        });
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDataList();
            }
        });
        mBinding.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.buttonClick()) {
                    return;
                } else {
                    isPublish = true;
                    showBottomZhaoCaiDialog(getActivity());
                }
            }
        });
    }

    @Override
    protected void init() {
        super.init();
        mModel.getAuctionHallTabList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isPublish){
            initDataList();
        }
    }

    public void setTabList(List<PublishListBean> data) {
        if (data == null && data.size() == 0) return;
        mFragments = new ArrayList<>();
        mTitles = new ArrayList<>();
        for (PublishListBean datum : data) {
            mFragments.add(AuctionHallFragment.newInstance("2", datum.getNo()));
            mTitles.add(datum.getName());
        }
        String[] titles = mTitles.toArray(new String[mTitles.size()]);
        mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        mBinding.viewPager.setOffscreenPageLimit(titles.length);
        mBinding.viewPager.setAdapter(mPagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                initDataList();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initDataList();
    }

    private void initDataList() {
        if (mBinding.tabLayout!=null && mFragments != null){
            int pos = mBinding.tabLayout.getSelectedTabPosition();
            Fragment fragment = mFragments.get(pos);
            if (fragment != null && fragment instanceof AuctionHallFragment) {
                AuctionHallFragment AuctionHallFragment = ((AuctionHallFragment) fragment);
                if (AuctionHallFragment.isInIt) {
                    AuctionHallFragment.setSearchName(mBinding.etView.getText().toString().trim());
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123456) {
            initDataList();
        }
    }

    /**
     * 招标竞价底部弹窗
     */
    public static void showBottomZhaoCaiDialog(Context context) {
        List<PublishMenuEntity> list = new ArrayList<>();
        list.add(new PublishMenuEntity("4", "招", "招标发布"));
        list.add(new PublishMenuEntity("5", "竞", "竞价发布"));
        list.add(new PublishMenuEntity("3", "拍", "拍卖发布"));
        list.add(new PublishMenuEntity("1", "项", "项目发布"));
        homezhaocaipublishmenupop = new HomeZhaoCaiPublishMenuPop(context, null);
        homezhaocaipublishmenupop.setData(list);
        homezhaocaipublishmenupop.setDialogClickCallBack(new HomeZhaoCaiPublishMenuPop.DialogClickCallBack() {
            @Override
            public void onItemClick(String name) {
                //0个人 1企业 2团队
                int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    ActivityJump.startActivity(PathLogin.VerificationCodeLoginActivity);
                } else {
                    switch (name) {
                        case "项目发布"://项目发布
                            if (type == 1) {
                                BaseApi.getInstance().startProjectIssue(context, 101);
                            } else {
                                MToast.showToast(context, "请切换至企业身份进行项目发布");
                            }
                            break;
                        case "拍卖发布"://拍卖发布
                            if (type == 1) {
                                BaseApi.getInstance().startAuctionPublish(context, 101);
                            } else {
                                MToast.showToast(context, "请切换至企业身份进行需求发布");
                            }
                            break;
                        case "招标发布": //供方发起
                            MToast.showToast(context, "移动端暂不支持发布招标，请前往电脑端`经营帮/用户中心/招采经营`发布招标信息");
                            break;
                        case "竞价发布": //供方发起
                            MToast.showToast(context, "移动端暂不支持发布竞价，请前往电脑端`经营帮/用户中心/招采经营`发布竞价信息");
                            break;
                    }
                }
            }
        }).show();
    }
}
